package db5;

import java.util.Scanner;

public class StudentSystem {
    static Student[] students = {
            new Student(1,"sala",18,"女"),
            new Student(2,"dany",19,"男"),
            new Student(3,"nimmi",20,"女")
    };
//    static Student[] students = new Student[2];
//    static Student s=new Student(1,"li",20,"男");
//    static Student s1=new Student(2,"mi",18,"女");

    //    全局使用  录入功能
    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        while (true){
            System.out.println("1.查看学生所有信息");
            System.out.println("2.输入姓名搜索学生信息");
            System.out.println("3.增加学生信息");
            System.out.println("4.修改学生信息");
            System.out.println("5.删除学生信息");
            System.out.println("6.请输入编号选择功能");
            System.out.println("退出");
            int index = input.nextInt();
            if (index==1){
//                students[0]=s;
//                students[1]=s1;
//                调用方法查看所有学生信息
                selectAll();

            } else if (index==2) {
//                调用方法搜索学生信息
                selectByName();

            } else if (index==3) {

                add();
            } else if (index==4) {


            } else if (index==5) {

                delete();
            } else if (index==6) {

            }else {
                System.out.println("没有此功能");
            }
        }
    }
    public static void selectAll(){
//        定义方法  查看所有学生信息
        if (students.length==0){
            System.out.println("没有学生信息");
        }else {
            for (int i = 0; i < students.length; i++) {
                Student student = students[i];
//                System.out.println(i);
                System.out.println("学号；"+student.no+"，姓名；"+student.name+"，年龄；"+student.age+"，性别；"+student.sex);
            }
        }
    }
    public static void selectByName(){
//        定义方法   查询学生信息
        System.out.println("请输入要查询的学生信息");
        String name = input.next();

        Student student=null;

        for (int i = 0; i < students.length; i++) {
//            如果查到的和表里的一样，循环停止
            if (name.equals(students[i].name)){
                student=students[i];
                break;
            }
        }
        if (student==null){
//            空就是没查到
            System.out.println("没有此学生");
        }else {
//            查到就输出
            System.out.println("学号；"+student.no+"，姓名；"+student.name+"，年龄；"+student.age+"，性别；"+student.sex);
        }
    }
    public static void add(){
//        1.创建学生对象，现在都是默认值；
        Student student=new Student();
//        2.通过录入器录入学生信息
        System.out.println("请输入编号");
        student.no=input.nextInt();
        //        3.校验是否重复；
        boolean isFind =false;
        for (int i = 0; i <students.length ; i++) {
            if(student.no==students[i].no){
                isFind=true;
                break;
            }

        }

//        判断是否为true
        if (isFind){
            System.out.println("学号重复");
        }else {
            System.out.println("请输入姓名");
            student.name = input.next();
            System.out.println("请输入年龄");
            student.age = input.nextInt();
            System.out.println("请输入性别");
            student.sex = input.next();

//            4.存入数据；地方不够（扩容数组）
            Student[] copy =new Student[students.length+1];
            for (int i = 0; i < students.length; i++) {
                copy[i]=students[i];
            }

            students=copy;

//            5.存入数组
            students[students.length-1] =student;
        }
    }
    public static void delete(){

    }
}
